﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace HouseOfTheFuture
{
    public partial class frmAddRoom : Form
    {
        bool isMoving;
        Point origLoc;
        Image origImage;
        Rectangle room;
        string database;
        int roomID;
        int floor;
        public frmAddRoom(string db, int floor_id)
        {
            InitializeComponent();
            database = db;
            floor = floor_id;
            SQLiteConnection conn = new SQLiteConnection("Data Source=" + db);
            SQLiteDataAdapter da = new SQLiteDataAdapter("Select floorplan from floors where floor_id = " + floor_id + ";", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])dt.Rows[0]["floorplan"]);
            Image img = Image.FromStream(ms);
            pictureBox1.Image = new Bitmap(img, pictureBox1.Width, pictureBox1.Height);
            origImage = pictureBox1.Image;
            ms.Close();
            ms.Dispose();
            ms = null;
            dt.Dispose();
            dt = null;
            da.Dispose();
            da = null;
            conn.Dispose();
            conn = null;
            roomID = -1;
        }
        public frmAddRoom(string db, int floor_id, int room_id)
        {
            InitializeComponent();
            database = db;
            floor = floor_id;
            SQLiteConnection conn = new SQLiteConnection("Data Source=" + db);
            SQLiteDataAdapter da = new SQLiteDataAdapter("Select floorplan, x,y,width, height from floors join rooms on rooms.floor_id = floors.floor_id where room_id = " + room_id + ";", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])dt.Rows[0]["floorplan"]);
            Image img = Image.FromStream(ms);
            pictureBox1.Image = new Bitmap(img, pictureBox1.Width, pictureBox1.Height);
            ms.Close();
            ms.Dispose();
            ms = null;
            room = new Rectangle(int.Parse(dt.Rows[0]["x"].ToString()), int.Parse(dt.Rows[0]["y"].ToString()), int.Parse(dt.Rows[0]["width"].ToString()), int.Parse(dt.Rows[0]["height"].ToString()));
            origImage = pictureBox1.Image;
            img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(img);
            g.Clear(Color.White);
            g.DrawImage(origImage, 0, 0, origImage.Width, origImage.Height);
            g.DrawRectangle(Pens.Red, room);
            pictureBox1.Image = img;
            pictureBox1.Invalidate();
            dt.Dispose();
            dt = null;
            da.Dispose();
            da = null;
            conn.Dispose();
            conn = null;
            roomID = room_id;
        }
        private void frmAddRoom_Load(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                ctl.ForeColor = this.ForeColor;
                ctl.BackColor = this.BackColor;
                if (ctl.GetType() == typeof(Button))
                {
                    ((Button)ctl).FlatAppearance.BorderColor = this.BackColor;
                }
                if (ctl.GetType() == typeof(Panel))
                {
                    foreach (Control child in ((Panel)ctl).Controls)
                    {
                        child.ForeColor = this.ForeColor;
                        child.BackColor = this.BackColor;
                        if (child.GetType() == typeof(Button))
                        {
                            ((Button)child).FlatAppearance.BorderColor = this.BackColor;
                        }
                    }
                }
            }
            MessageBox.Show("Draw a rectangle around the room");
            
        }

        void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isMoving = false;
        }

        void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (isMoving)
            {
                int x;
                int y;
                int width = Math.Abs(e.X - origLoc.X);
                int height = Math.Abs(e.Y - origLoc.Y);
                if (origLoc.X > e.X)
                {
                    x = e.X;
                }
                else
                {
                    x = origLoc.X;
                }
                if (origLoc.Y > e.Y)
                {
                    y = e.Y;
                }
                else
                {
                    y = origLoc.Y;
                }
                room = new Rectangle(x, y, width, height);
                Image img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                Graphics g = Graphics.FromImage(img);
                g.Clear(Color.White);
                g.DrawImage(origImage, 0, 0, origImage.Width, origImage.Height);
                g.DrawRectangle(Pens.Red, room);
               
                pictureBox1.Image = img;
                pictureBox1.Invalidate();
            }
        }

        void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isMoving = true;
            origLoc = e.Location;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SQLiteConnection conn = new SQLiteConnection("Data Source=" + database);
            SQLiteDataAdapter da;
            if (roomID > -1)
            {
                da = new SQLiteDataAdapter("update rooms set x = " + room.X + ", y = " + room.Y + ", width = " + room.Width + ", height = " + room.Height + " where room_id = " + roomID + ";", conn);
            }
            else
            {
                InputResult result = inputBox.getInput("What is the name of this room?");
                da = new SQLiteDataAdapter("insert into rooms values (null, \'" + result.Text + "\'," + floor + "," + room.X + "," + room.Y + "," + room.Width + "," + room.Height + ");", conn);
            }
            da.Fill(new DataTable());
            da.Dispose();
            da = null;
            conn.Dispose();
            conn = null;
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
